package com.enjoyor.soa.traffic.server.tsms.dubbo.listener;

import java.util.Iterator;
import java.util.Map;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.enjoyor.soa.traffic.server.tsms.common.RehearseGlobal;
import com.enjoyor.soa.traffic.server.tsms.handler.InitializeHandler;
import com.enjoyor.soa.traffic.server.tsms.thead.RehearseThead;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;

/**
 *
 * 定时清理线程、缓存等系统数据
 * @Description TODO
 * @Author fzj
 * @Date 2019/7/25 9:41
 * @Version 1.0
 **/
@Component
public class ClearSystemDataService {

	/**
	 * 清理预演线程
	 */
	//@Scheduled(cron="0 */5 * * * ?")
	@Scheduled(cron="0 0 ${clear.system.data.period} * * ?")
    public void clearRehearseThread(){
        try {
    		Iterator<RehearseThead> iterator = RehearseGlobal.rehearseThreadlist.iterator();
    		while (iterator.hasNext()) {
    			RehearseThead thread = iterator.next();
    			if (thread.getState() == Thread.State.WAITING || thread.getState() == Thread.State.TIMED_WAITING) {
    				thread.interrupt();
    				iterator.remove();
    			}
    		}
    		LoggerHelper.LOG.info(TimeHelper.dateToLongString()+"------定时清理预演线程--------");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	/**
	 * 清理预演数据
	 */
	//@Scheduled(cron="0 */5 * * * ?")
	@Scheduled(cron="0 0 ${clear.system.data.period} * * ?")
    public void clearRehearsePoolData(){
        try {
        	RehearseGlobal.RehearseDataPool.clear();
        	LoggerHelper.LOG.info(TimeHelper.dateToLongString()+"------定时清理预演数据--------");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	
	/**
	 * 清理锁定设备数据
	 */
	//@Scheduled(cron="0 */5 * * * ?")
	@Scheduled(cron="0 0 ${clear.system.data.period} * * ?")
    public void clearDeviceLockData(){
        try {
        	RehearseGlobal.VideoLockPool.clear();
        	RehearseGlobal.SignalLockPool.clear();
        	LoggerHelper.LOG.info(TimeHelper.dateToLongString()+"------定时清理锁定设备数据--------");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	
	/**
	 * 清理方案执行线程数据
	 */
	//@Scheduled(cron="0 */${clear.system.data.period} * * * ?")
	@Scheduled(cron="0 0 ${clear.system.data.period} * * ?")
    public void clearActionData(){
        try {
        	Map<String, Map<String,Object>> actionDataMap = RehearseGlobal.ActionDataPool;
        	for (Map.Entry<String, Map<String, Object>> m : actionDataMap.entrySet()) {
        		String executeTaskId = m.getKey();
    			RehearseGlobal.ActionDataPool.get(executeTaskId).put("stop",true);
        		if(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listerid")!=null){//删除定位订阅
        			long listerid = Long.parseLong(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listerid").toString());
        			InitializeHandler.getInstance().getPositionService().removeListener(listerid);
    			}
    			if(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listenerIdPass")!=null){//删除卡口过车数据订阅
    				InitializeHandler.getInstance().getVehicleCallbackService().removeVehiclePassListener(executeTaskId);
    			}
    			RehearseGlobal.ActionDataPool.remove(executeTaskId);
        	}
        	//RehearseGlobal.ActionDataPool.clear();
        	LoggerHelper.LOG.info(TimeHelper.dateToLongString()+"------定时清理方案执行线程--------");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
